<?php
	//Attempts to get an array of information from a SC2Ranks URL
	function get_sc_ranks_info($url){
		
		//return array( '0' => 'broken');
		
		//try to open connection to web page
		if( !($fd = fopen($url,"r")) )
   			return false;
   			
		$data = array();
		$i=0;
		while( $buf = fgets($fd,1024) )
		{
			$data[$i++] = $buf;
		}
		
		fclose($fd);
		
		//grabs the line from the page thats relevant 
		$pattern = '(1 vs 1)';
		$matches = preg_grep($pattern, $data);
		
		//to get the key, since grep uses the same key from the $data array
		$key;
		foreach($matches as $k => $val){
			$key = $k;
		}
				
		//extract information from relevant line
		$ss = 'class="summary topborder" colspan="2"><span class="number">';
		$es = '% wins)';
		$short = _subs($matches[$key], $ss, false, $es, true);
		//return $short;
		//return _subs($short, '</span> (', false, '% wins)', false);
		
		$data['league'] = _subs($matches[$key], 'rowspan="2"><img alt="', false, '-medium" height=', false);
		$data['score']= substr($short, 0, strpos($short, '</span> points.'));
		$data['wins']= _subs($short, '<span class="green">', false, '</span>, lost', false);
		$data['losses']= _subs($short, '<span class="red">', false, '</span> (', false);
		$data['ratio']= _subs($short, '</span> (', false, '% wins)', false);
		$data['name']=_subs($data[$key+1], 'rel="canonical">', false, '</a></td><td class="character">&nbsp;</td><', false);
		
		
		
		return $data;
	
	}
	
	/**Gets the html string to display the picture from sc2 Ranks*/
	function get_picture($url){
		//try to open connection to web page
		if( !($fd = fopen($url,"r")) )
   			return false;
   			
		$data = array();
		$i=0;
		while( $buf = fgets($fd,1024) )
		{
			$data[$i++] = $buf;
		}
		
		fclose($fd);
		
		$pattern = '(portraits)';
		$matches = preg_grep($pattern, $data);
		
		//to get the key, since grep uses the same key from the $data array
		$key;
		foreach($matches as $k => $val){
			$key = $k;
		}
		
		$str = $matches[$key];
		
		$pre = _subs($str, '<span', false, '/images/portraits', false);
		$mid = 'http://www.sc2ranks.com';
		$suf = _subs($str, '/images/portraits', true, '</span>', false );
		
		return '<div '.$pre.$mid.$suf.'</div>';
		
	
	
	}
	
	
	/**
	 * Returns all chars after $prefix and up to $suffix (includes the prefix and suffixes based on booleans) in $string
	 * */
	function _subs($string, $prefix, $includePref, $suffix, $includeSuf){
		$s = $includePref ? 0 : strlen($prefix);
		$e = $includeSuf ? strlen($suffix) : 0;
		$start = strpos($string, $prefix) + $s;
		$end = strpos($string, $suffix) +  $e;
		$short = substr($string, $start , $end-$start);
		return $short;
	}
	
	function get_bnet_name(){
	
	}
	
	/**
	 * gets the sc2ranks url from the bnet url
	 * argument is battle.net link. if not provided uses logged user
	 * */
	function get_ranks_url($bnet = ''){
		//return 'blah';
		if($bnet == ''){
			$data = $this->session->userdata('profile_data');
			$bnet = $data['bnet'];	
		}
		
		$bnet_id = substr($bnet, 36);
		$bnet_id=substr_replace($bnet_id, substr(strstr($bnet_id, '1/'), 2), 8);
		return 'http://www.sc2ranks.com/us/'.$bnet_id;
	}
